function CarModel(places) {
	
	var cm = this;        
    this.places = places;
	
    this.getPlaces = function() {
        var res = new Object();
        var i = 0;
        for (var key in this.places) {
            if (this.places[key].action == "delete") {
                res[this.places[key].id] = this.places[key].name;
                i++;
            }
        }
        return res;
    },
        
    this.changePlace = function(id, action, callback) {
        
        for (var key in this.places) {
            if (this.places[key].id == id) {
                this.places[key].action = action;
                break;
            }
        }
        callback(id);
	},
    
    this.add = function(row) {
		
        var json_data = JSON.stringify({
            model:      row.model, 
            output_year:row.output_year, 
            insurance:  row.insurance, 
            price:      row.price, 
            start_place:row.start_place,
            places:     this.getPlaces()
        });
        $.ajax({
            type: "POST",
            url: "car_add.htm",
            data: {
                data: json_data
            },
            dataType: "json",
            success: function(response) {
                if (response.res == "false") {
                    alert(response.error);
                }
                window.location.href = "my_cars_list.htm";
            }
        });
	},
        

	
	this.edit = function(row) {

        var json_data = JSON.stringify({
            id:         row.id,
            model:      row.model, 
            output_year:row.output_year, 
            insurance:  row.insurance, 
            price:      row.price, 
            start_place:row.start_place,
            places:     this.getPlaces()
        });
        $.ajax({
            type: "POST",
            url: "car_edit.htm",
            data: {
                data: json_data
            },
            dataType: "json",
            success: function(response) {
                if (response.res == "false") {
                    alert(response.error);
                }
                window.location.href = "my_cars_list.htm";
            }
        });
	}
}